

/**
 * @file display.cpp
 * @brief human body health monitoring sensor
 * @author achdu0000@163.com
 * @version 1.0.0
 * @date 2024/07/04
 */

#include "display.h"

static String title = "Hello world, achdu0000!";

static bool isClicked = false;
static void event_handler_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *btn = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED)
    {
        LV_LOG_INFO("btn is clicked");
        lv_obj_t *label = lv_obj_get_child(btn, 0);
        if (label)
        {
            lv_label_set_text(label, isClicked ? "Not Clciked" : "Clicked");
            isClicked = !isClicked;
        }
    }
}

static void create_my_display()
{
        lv_obj_t*circle = lv_arc_create(lv_scr_act());
    lv_obj_set_size(circle, ESP_PANEL_LCD_LEN, ESP_PANEL_LCD_LEN);
    lv_obj_center(circle);
    lv_arc_set_bg_angles(circle,0,360);
    lv_arc_set_range(circle, 0, 360);

    // 设置 arc 的颜色和宽度
    lv_obj_set_style_arc_color(circle, lv_color_black(), 0);
    lv_obj_set_style_arc_width(circle, 2, 0);

        // 隐藏 arc 的背景
    lv_obj_set_style_bg_opa(circle, LV_OPA_TRANSP, 0);

    lv_obj_t *title_label = lv_label_create(lv_scr_act());

    lv_label_set_text(title_label, title.c_str());
    lv_obj_align(title_label, LV_ALIGN_CENTER, 0, 0);

    static lv_style_t my_style;
    lv_style_init(&my_style);
    lv_style_set_text_font(&my_style, &lv_font_montserrat_30);
    lv_obj_add_style(title_label, &my_style, 0);

    lv_obj_t *label;

    lv_obj_t *btn1 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn1, event_handler_cb, LV_EVENT_ALL, NULL);
    lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -80);

    label = lv_label_create(btn1);
    lv_label_set_text(label, "Not Clicked");
    lv_obj_center(label);


}

void log_cb(const char *buf)
{
    _LOG_SERIAL.println(buf);
}

void onCreate()
{
    lv_log_register_print_cb(log_cb);

    String log_msg = title + "starting";
    LV_LOG_INFO(log_msg.c_str());

    LV_LOG_INFO("Init panel device");

    ESP_Panel *panel = new ESP_Panel();
    panel->init();

#if LVGL_PORT_AVOID_TEAR
    // When avoid tearing function is enabled, configure the RGB bus according to the LVGL configuration
    ESP_PanelBus_RGB *rgb_bus = static_cast<ESP_PanelBus_RGB *>(panel->getLcd()->getBus());
    rgb_bus->configRgbFrameBufferNumber(LVGL_PORT_DISP_BUFFER_NUM);
    rgb_bus->configRgbBounceBufferSize(LVGL_PORT_RGB_BOUNCE_BUFFER_SIZE);
#endif

    panel->begin();

    LV_LOG_INFO("Init LVGL");
    lvgl_port_init(panel->getLcd(), panel->getTouch());

    LV_LOG_INFO("Create UI");
    /** Lock the mutex due to the LVGL APIs are not thread-safe */
    lvgl_port_lock(-1);

    create_my_display();

    /** Release the mutex */
    lvgl_port_unlock();

    log_msg = title + "ending";
    LV_LOG_INFO(log_msg.c_str());
}
